<?php
class Lib2k_Parsing_Filmix {
    
    private $_url = 'http://filmix.net';
  //  private $_url = 'http://www.rzd-partner.ru/';
    private $_contentHTML;
    
 
    /** get html from url */
    private function _getData($url){
		$Connect = new Lib2k_Parsing_cUrl();
    	$Connect->get($url);
        $contentText = $Connect->getHtml();
      //  $contentText = iconv("windows-1251", "utf-8", $contentText);
        
        $this->_contentHTML =  $contentText;
        return $contentText;
    }
    
    /** get html of documentElement */
    private function _getInnerHTML($Node)
    {
         $Body = $Node;
         $Document = new DOMDocument('1.0','utf-8');    
         $Document->appendChild($Document->importNode($Body,true));
         return $Document->saveHTML();
    }  
    
    /** delete whitespaces - better then trim */
    private function _mb_trim( $string )
    {
        $string = preg_replace( "/(^\s+)|(\s+$)/us", "", $string );
       
        return $string;
    }   
    
    private function _removeHtmlTagA($html){
        $html = preg_replace('#<a(.*?)>(.*?)</a>#is', '', $html);
        return $html;
    }    
    
    /** parse latest telegrams from main page */
    public function parsePage($page = null){
        if($page > 1){
            $url = $this->_url . '/page/' . $page;    
        } else {
            $url = $this->_url;
        }
       // Zend_Debug::dump($url);
        $html = $this->_getData($url);
      //  Zend_Debug::dump(htmlspecialchars($html));
      //  echo $html;
       // return false;
        $globalData = null; //  all items of page
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('#dle-content div.block');
       // Zend_Debug::dump(count($results));
        foreach ($results as $result) {
            $htmlItem = $this->_getInnerHTML($result);

            $domItem = new Zend_Dom_Query($htmlItem);
            $data = null;
            /** title */
            $resultItems = $domItem->query('h2 a');
            $resItemCur = $resultItems->current(); 
            if($resItemCur) {
                $data['title'] = $resItemCur->nodeValue;
            } else {
                $data['title'] = null;
            }
            /** link */
            if($resItemCur) {
                $data['link'] = $resItemCur->getAttribute('href');
            } else {
                $data['link'] = null;
            }                       
            /** rating */
            $resultItems = $domItem->query('li.rating b');
            $resItemCur = $resultItems->current();
            if($resItemCur) { 
                $data['rating'] = intval($resItemCur->nodeValue);
            } else {
                $data['rating'] = null;
            }                        
            
            /** img */
            $resultItems = $domItem->query('.body .post img');
            $resItemCur = $resultItems->current(); 
            if($resItemCur) {
                //$data['image'] = $this->_url . $resItemCur->getAttribute('src');
                $src =  $resItemCur->getAttribute('src');                               
                if(mb_strpos($src, "http") === false){
                    $data['image'] =  $this->_url . $src;    
                } else {
                    $data['image'] =  $src;
                }
                
                   
            } else {
                $data['image'] = null;
            }
                        
            
            /** description */
            $resultItems = $domItem->query('.body .post > div > div');
            $resItemCur = $resultItems->current();
            if($resItemCur) { 
                $data['description'] = $resItemCur->nodeValue;
            } else {
                $data['description'] = null;
            }   
            
            /** categories */
            $resultItems = $domItem->query('li.genre a');
            $categories = null;
            if(count($resultItems) > 0) {
                foreach($resultItems as $cat) {
                    $catItem['link'] = $cat->getAttribute('href');
                    $catItem['title'] = $cat->nodeValue;
                    $categories[] = $catItem;
                }
            } else {
                $categories = null;
            }             
            $data['categories'] = $categories;                          
                      
            $globalData[] = $data;                                                                           
        }        
        return $globalData;
    }
    

}